<?php

/**
 * IndexController - The default controller class
 *
 * @author
 * @version
 */

require_once 'Zend/Controller/Action.php';

class Administrator_OrderController extends Zend_Controller_Action {



    public function init() {



    }

    public function preDispatch() {

        $this->getHelper ('layout')->setLayout('framelayout');

        $this->view->addHelperPath(MY_ROOT.'/vhelpers');

        Zend_Controller_Action_HelperBroker::addPath(MY_ROOT.'/chelpers');

        $member=new CMember();

        if(!$member->isUser($this->_request->getModuleName(),$this->_request->getControllerName())) {

            $this->_redirect(MY_URL.'/administrator-auth-login');

            return false;

        }

        return true;

    }


    public function indexAction() {

        $orderfl=new CFenlei('order');

        $fenleiArray=$orderfl->getAllFenlei();

        $orderfl->getAll($fenleiArray,0);

        $fenleiString=str_replace(array('<div class="smallChildDiv"></div>','<div></div>'),array('',''),$orderfl->fenleiString);

        $this->view->assign('fenleiString',$fenleiString);

        $this->view->assign('fenleiArray',$fenleiArray);

    }

    public function getitemajaxAction() {

        $this->getHelper ('layout')->disableLayout ();

        $flid=$this->_request->getParam('flid',0);

        $page=$this->_request->getParam('page',1);

        $searchQuery=$this->_request->getParam('searchQuery','');

        $searchArray=json_decode(stripslashes($searchQuery),true);

        $order=new COrder();

        $where=array();

        if($flid!=0) {

            $where[]=R_PRE."orders.flid='$flid'";

        }

        if($searchArray['orderid']!='') {

            $where[]="orderid='".$searchArray['orderid']."'";

        }

        if($searchArray['sendid']!='') {

            $where[]="sendid='".$searchArray['sendid']."'";

        }

        if($searchArray['time_from']!='') {

            $searchArray['time_from']=strtotime($searchArray['time_from']);

            $where[]="add_time>'".$searchArray['time_from']."'";

        }

        if($searchArray['time_to']!='') {

            $searchArray['time_to']=strtotime($searchArray['time_to']);

            $where[]="add_time<'".$searchArray['time_to']."'";

        }

        if($searchArray['buyer']!='') {

            $member=new CMember();

            $userArray=$member->getUserDetails(array('username'=>$searchArray['buyer']));

            $where[]=R_PRE."orders.uid='".$userArray[0]['uid']."'";

        }

        if($searchArray['seller']!='') {

            $member=new CMember();

            $userArray=$member->getUserDetails(array('username'=>$searchArray['seller']));

            $where[]="seller_uid='".$userArray[0]['uid']."'";

        }

        if($searchArray['money_from']!='') {

            $where[]="money>'".$searchArray['money_from']."'";

        }

        if($searchArray['money_to']!='') {

            $where[]="money<'".$searchArray['money_to']."'";

        }

        if($searchArray['flid2']!='0'&&$searchArray['flid2']!='') {

            $where[]=R_PRE."orders.flid='".$searchArray['flid2']."'";

        }

        $where=join(' AND ',$where);

        $orderCounts=$order->getAllOrderCounts(0,$where);

        Zend_Loader::loadClass("CFilter");

        $pageArray = CFilter::filterPage ( $page, $orderCounts, 10 );

        $orderArray=$order->getAllOrders($pageArray['from'],10,0,$where);

        Zend_Loader::loadClass("CPage");

        $pageString = CPage::commonDivAjax($pageArray ['pageNumber'], $pageArray ['page'], 'productEffection.showAll',$flid); //生成分页字符串

        $this->view->assign ( "pageString", $pageString );

        $this->view->assign ( "pageArray", $pageArray );

        $this->view->assign ( 'orderArray', $orderArray );

    }

    public function docancelAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $order=new COrder();

        $orderArray=$order->getDetail($orderid);

        if($orderArray[0]['flid']=='1') {

            $order->editOrder($orderid,array('flid'=>7));

            $this->view->showMsg('订单正常取消!');

        }else if($orderArray[0]['flid']=='2') {

                $order->editOrder($orderid,array('flid'=>8));

                $this->view->showMsg('订单非正常取消! ');

            }else if($orderArray[0]['flid']=='7'||$orderArray[0]['flid']=='8') {

                    $this->view->showMsg('订单已经取消了!');

                }else {

                    $this->view->showMsg('订单不能取消!');

                }

    }

    public function moreAction() {

        $orderid=$this->_request->getParam('orderid');

        $order=new COrder();

        $CLagnappe=new CLagnappe();
        $attachment3=new CLagnappeAttachment();
        $orderArray=$order->getDetail($orderid);

        if($orderArray[0]["lagnappe"]) {
            $LagnappeArray=$CLagnappe->getDetail($orderArray[0]["lagnappe"]);
            $attachmentArray3=$attachment3->getAttachments($orderArray[0]["lagnappe"]);
        }

        $this->view->assign('orderArray',$orderArray);

        $this->view->assign('attachmentArray3',$attachmentArray3);

        $this->view->assign('LagnappeArray',$LagnappeArray);

        $itemArray=array();
        //die($orderArray[0]['order_string']);
        //eval('$itemArray='.$orderArray[0]['order_string'].';');
        $itemArray=unserialize(stripslashes($orderArray[0]['order_string']));

        $this->view->assign('itemArray',$itemArray);

        $orderfl=new CFenlei('order');

        $orderStateArray=$orderfl->getFenleiByPid(0);

        $this->view->assign('orderStateArray',$orderStateArray);

        $attachment=new COrderAttachment();

        $attachmentArray=$attachment->getAttachments($orderid);

        $this->view->assign('attachmentArray',$attachmentArray);

        $attachment->setIsSeller(0);

        $attachmentArray2=$attachment->getAttachments($orderid);

        $this->view->assign('attachmentArray2',$attachmentArray2);

        $orderRemark=new COrderRemark();

        $disputeRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'1'));

        $this->view->assign('disputeRemarkArray',$disputeRemarkArray);

        $reimburseRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'2'));

        $this->view->assign('reimburseRemarkArray',$reimburseRemarkArray);

        $cancelDisputeRemarkArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'3'));

        $this->view->assign('cancelDisputeRemarkArray',$cancelDisputeRemarkArray);

        $cancelReimbursementArray=$orderRemark->getRemarkDetails(array('orderid'=>$orderid,'flid'=>'4'));

        $this->view->assign('cancelReimbursementArray',$cancelReimbursementArray);

        $orderItem=new COrderItem();

        $orderItemArray=$orderItem->getDetail($orderid,$itemArray[0]['itemid']);

        $this->view->assign('orderItemArray',$orderItemArray);

        $orderDelay=new COrderDelay();

        $orderDelayArray=$orderDelay->getDelayDetails(array('orderid'=>$orderid));

        $this->view->assign('orderDelayArray',$orderDelayArray);

        $account=new CAccount();

        $reimburseAccountArray=$account->getAccountDetails(array('orderid_string'=>serialize(array($orderid)),'flid'=>5));

        $this->view->assign('reimburseAccountArray',$reimburseAccountArray);

        $Cyunfei=new Cyunfei();

        $oneyunfeiArray=$Cyunfei->getDetail($orderArray[0]["yunfeiid"]);

        $this->view->assign('oneyunfeiArray',$oneyunfeiArray);

    }

    public function uploadimgAction() {

        header("Content-type: text/html; charset=utf-8");

        $this->getHelper ('layout')->disableLayout ();

        $orderid=$this->_request->getParam('orderid');

        $upload=new CUpload();

        $path=new CPath();

        $image=new CImage();

        $thisPath=$path->getPathFromType('item');

        $imageArray=$upload->uploadFile($thisPath,null,null,'500000');

        $cutPath=$image->cutPhoto($thisPath, '200', $path->getPathFromType('cut_item'));//截图

        $isSuccess=false;

        if(is_array($imageArray)) {

            $isSuccess=true;

            $imgUrl=$path->toDbPath('item',$imageArray[0]);

            $this->view->assign('imgUrl',$imgUrl);

            $attachment=new COrderAttachment();

            $data=array(
                'orderid'=>$orderid,
                'attachment_url'=>$imgUrl,
                'intro'=>'',
                'cut_url'=>$cutPath,
                'ordering'=>0,
                'is_seller'=>0,
            );

            $attachmentid=$attachment->createAttachments($data);

            $this->view->assign('attachmentid',$attachmentid);

        }else {

            $error=$imageArray;

            $this->view->assign('error',$error);

        }

        $this->view->assign('isSuccess',$isSuccess);

    }

    public function delimgbyidAction() {

        $this->_helper->viewRenderer->setNoRender();

        $this->getHelper ('layout')->disableLayout ();

        $attachmentid=$this->_request->getParam('attachmentid');

        $attachment=new COrderAttachment();

        $attachmentArray=$attachment->getAttachmentByAttachmentid($attachmentid);

        $attachment_url=$attachmentArray['attachment_url'];

        $cut_url=$attachmentArray['cut_url'];


        $path=new CPath();

        @unlink($path->toAbsoluteRootPath($attachment_url));

        @unlink($path->toAbsoluteRootPath($cut_url));



        $attachment->delAttachmentsById(array($attachmentid));

        echo 'success';


    }


    public function expressAction() {

        $orderid=$this->_request->getParam('orderid');

        $itemid=$this->_request->getParam('itemid');

        $order=new COrder();

        $orderArray=$order->getDetail($orderid);

        $this->view->assign('orderArray',$orderArray);

        $orderContact=new COrderContact();

        $buyerContactArray=$orderContact->getContactByUid($orderArray[0]['uid']);

        $this->view->assign('buyerContactArray',$buyerContactArray);

        $this->view->assign('orderid',$orderid);

        $this->view->assign('itemid',$itemid);

    }

    public function doconfirmAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $itemid=$this->_request->getParam('itemid');

        $orderItem=new COrderItem();

        $orderItem->editOrderItem($orderid,$itemid,array('has_shipped'=>1));

        $orderItemCounts=$orderItem->getOrderItemCounts($orderid);

        $order=new COrder();

        $orderArray=$order->getDetail($orderid);

        $itemArray=array();

        //eval('$itemArray='.$orderArray[0]['order_string'].';');
        $itemArray=unserialize($orderArray[0]['order_string']);

        if($orderItemCounts==count($itemArray)) {

            $order->editOrder($orderid,array('flid'=>4,'ship_time'=>time()));

        }


        $this->view->showMsg('确认已经发货!');

    }

    public function setoderflAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $flid=$this->_request->getParam('flid');

        $order=new COrder();

        $order->editOrder($orderid,array('flid'=>$flid));

        $this->view->showMsg('更改状态成功!');

    }

    public function setstateAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $state=$this->_request->getParam('state');

        $order=new COrder();

        $order->editOrder($orderid,array('flid'=>$state));

        $this->view->showMsg('更改状态成功!',$_SERVER['HTTP_REFERER']);

    }

    public function setcompleteAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $order=new COrder();

        $order->editOrder($orderid,array('flid'=>9));

        $this->view->showMsg('成功把订单设为完成状态!',$_SERVER['HTTP_REFERER']);

    }

    public function setshippedAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $order=new COrder();

        $order->editOrder($orderid,array('flid'=>4));

        $this->view->showMsg('成功把订单设为已发货状态!',$_SERVER['HTTP_REFERER']);

    }
    public function setsendidAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');
        $usernameemail=$this->_request->getParam('usernameemail');
		
        $yunfeicarrier=$this->_request->getParam('yunfeicarrier');
		$transittime=$this->_request->getParam('transittime');
		$website=$this->_request->getParam('website');
		
        $sendid=$this->_request->getParam('sendid');
        $order=new COrder();

        $order->editOrder($orderid,array('sendid'=>$sendid));



        $config=new CConfigorderemail();

        $array=$config->getAllConfigorderemail();

        $configArray=array();

        foreach ($array as $value) {

            $configArray[$value['name']]=$value['data'];

        }

        $to = $usernameemail;
        $subject = $configArray['EMAIL_TITLE'];
        $message = str_replace("{#sendid}",$sendid.' '.$yunfeiinfo,(str_replace("{#orderid}", $orderid, $configArray['EMAIL_BODY'])));
		$message = str_replace("\r\n","<br>",$message);
		$message = str_replace("{#Carrier}",$yunfeicarrier,$message);
		$message = str_replace("{#TransitTime}",$transittime,$message);
		$message = str_replace("{#WebSite}",$website,$message);
		
		$message = str_replace("\r\n","<br>",$message);
        $from = $configArray['EMAIL_BOX'];
        $headers = "MIME-Version: 1.0" . "\r\n";
        $headers .= "Content-type:text/html;charset=iso-8859-1" . "\r\n";
        $headers .= "From: $from";
        mail($to,$subject,$message,$headers);

        $this->view->showMsg('成功输入运单号!,并了发邮件,给客户',$_SERVER['HTTP_REFERER']);

    }

    public function doapproveAction() {

        $this->_helper->viewRenderer->setNoRender();

        $has_approved=$this->_request->getParam('has_approved');

        $orderid=$this->_request->getParam('orderid');

        $orderDelay=new COrderDelay();

        $data=array('has_approved'=>$has_approved);

        $orderDelay->editDelay($orderid,$data);

        $order=new COrder();

        $orderDelayArray=$orderDelay->getDelayDetails(array('orderid'=>$orderid));

        if($has_approved=='1') {

            $order->updateDeliveryDay($orderDelayArray[0]['delay_days'],$orderid,true);

        }

        $this->view->showMsg('设置成功！',$_SERVER['HTTP_REFERER']);

    }

    public function doaccidentcostAction() {

        $this->_helper->viewRenderer->setNoRender();

        $orderid=$this->_request->getParam('orderid');

        $accident_cost=$this->_request->getParam('accident_cost');

        $accident_cost_reason=$this->_request->getParam('accident_cost_reason');

        $order=new COrder();

        $order->editOrder($orderid,array('accident_cost'=>$accident_cost,'accident_cost_reason'=>$accident_cost_reason));

        $this->view->showMsg('设置成功！',$_SERVER['HTTP_REFERER']);

    }

    public function postAction() {

    //$this->_helper->viewRenderer->setNoRender();

    //$this->getHelper ('layout')->disableLayout ();

    }



}
